/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.task;

import com.appspot.ittrick.drive.trick.api.DriveAPIService;
import com.appspot.ittrick.drive.trick.controller.MainController;
import com.appspot.ittrick.drive.trick.model.FileModel;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;
import java.util.ArrayList;
import java.util.List;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class LaodFilesListTask extends Service<List<FileModel>> {
    
    public enum RunModeEnum {loadFilesByFolderId, loadTrashFiles, loadSearchFiles, loadParentDriFiles, loadSharedWithMeFiles};
    
    private RunModeEnum runmode = null;
    
    private File rootDri;
    private String searchString;
    
    public void reLoad() {
        if(runmode != null) {
            this.restart();
        }
    }
    
    public void loadFilesByFolderId(File rootDri) {
        this.rootDri = rootDri;
        runmode = RunModeEnum.loadFilesByFolderId;
        this.restart();
    }
    
    public void loadSearchFiles(String searchString) {
        this.searchString = searchString;
        runmode = RunModeEnum.loadSearchFiles;
        this.restart();
    }
    
    public void loadTrashFiles() {
        runmode = RunModeEnum.loadTrashFiles;
        this.restart();
    }
    
    public void loadParentDriFiles() {
        if(runmode == RunModeEnum.loadFilesByFolderId || runmode == RunModeEnum.loadParentDriFiles) {
            runmode = RunModeEnum.loadParentDriFiles;
            this.restart();
        }
    }
    
    public void loadSharedWithMeFiles() {
        runmode = RunModeEnum.loadSharedWithMeFiles;
        this.restart();
    }

    @Override
    protected Task<List<FileModel>> createTask() {
        return new Task<List<FileModel>>() {

            @Override
            protected List<FileModel> call() throws Exception {
                DriveAPIService driveService = MainController.getInstance().getDriveService();
                
                List<FileModel> fileModelList = new ArrayList();
                List<File> loadFileList = null;
                
                if(runmode == RunModeEnum.loadFilesByFolderId) {
                    loadFileList = driveService.getChildrenFilesList(rootDri.getId());
                    
                }else if(runmode == RunModeEnum.loadSearchFiles) {
                    loadFileList = driveService.searchFileList(searchString);
                    
                }else if(runmode == RunModeEnum.loadTrashFiles) {
                    loadFileList = driveService.getTrashFilesList();
                    
                }else if(runmode == RunModeEnum.loadParentDriFiles) {
                    
                    List<ParentReference> parentList = rootDri.getParents();

                    if (parentList != null && parentList.size() > 0) {
                        String parentId = parentList.get(0).getId();
                        File parentFile = driveService.getFile(parentId);
                        rootDri = parentFile;
                    }
                    
                    loadFileList = driveService.getChildrenFilesList(rootDri.getId());
                    
                }else if(runmode == RunModeEnum.loadSharedWithMeFiles) {
                    loadFileList = driveService.getSharedWithMeFilesList();
                }
                
                for(File file: loadFileList) {
                    fileModelList.add(new FileModel(file));
                }
                
                return fileModelList;
            }
        };
    }
    
    public File getfolderFile() {
        return rootDri;
    }
    
}
